<template>
  <el-dialog
    title="添加员工"
    :visible.sync="show"
    width="600px"
    @close="closeBtn"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" type="date" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment">
          <el-option
            v-for="item in employeesData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="form.departmentName" readonly @focus="focusEvent" />
        <el-tree
          v-if="treeShow"
          :data="treeData"
          :props="{ label: 'name' }"
          @node-click="nodeClick"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="选择日期"
          style="width: 100%"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer" style="text-align: center">
      <el-button @click="closeBtn">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import employeesData from '@/api/constant/employees'
import { department } from '@/api/treeitem.js'
import { sysUserPost } from '@/api/employees'
export default {
  data() {
    const phone = (r, v, c) => {
      const reg =
        /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
      if (!reg.test(v)) {
        c('请输入正确的手机号')
      } else {
        c()
      }
    }
    return {
      employeesData,
      treeShow: false,
      treeData: [],
      show: false,
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          { validator: phone, trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'change' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      },
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    }
  },
  methods: {
    async focusEvent() {
      this.treeShow = true
      if (this.treeData.length !== 0) return
      const res = await department()
      this.treeData = this.getnewData(res.data.depts, '')
    },
    nodeClick(data) {
      this.treeShow = false
      this.form.departmentName = data.name
    },
    getnewData(arr, pid = '') {
      return arr.filter((v) => {
        if (v.pid === pid) {
          v.children = this.getnewData(arr, v.id)
          return true
        }
      })
    },
    submit() {
      this.$refs.form.validate(async(v) => {
        if (v) {
          await sysUserPost(this.form)
          this.$message.success('成啦！')
          this.$emit('getData')
          this.closeBtn()
        }
      })
    },
    closeBtn() {
      this.show = false
      this.$refs.form.resetFields()
      this.form = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
